﻿using Payroll.BusinessLogic.Discounts;
using Payroll.Common.Utils;
using PayrollHR.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PayrollHR.Administration.Modules
{
    public partial class DiscountControl : BasePayrollAdministrationUserControl
    {
        public int CustomerID
        {
            get
            {
                return CommonHelper.QueryStringInt("CustomerId");
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DiscountBind();
            }
        }
        private void DiscountBind()
        {
            var discountColection = this.DiscountService.GetAllDiscountById(CustomerID);
            gvDiscount.DataSource = discountColection;
            gvDiscount.DataBind();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try {
                string discountid = "";
                foreach (GridViewRow row in gvDiscount.Rows)
                {
                    var cbDiscountID = row.FindControl("cbDiscountID") as CheckBox;
                    bool isChecked = cbDiscountID.Checked;
                    if (isChecked)
                    {
                        discountid += gvDiscount.DataKeys[row.RowIndex].Value + ",";
                    }
                }
                if (discountid.Length > 0)
                {
                    discountid = discountid.TrimEnd(',');
                    this.DiscountService.Delete(discountid);
                    this.DiscountBind();
                }
            }
            catch (Exception exc)
            {
                ProcessException(exc);
            }
        }

        protected void AddButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (CommonHelper.IsNull(DateApplyDatePicker.SelectedDate))
                {
                    throw new ArgumentNullException("customerSession");
                }
                Discount discount = new Discount
                {
                    DiscountPercentage = CommonHelper.ToDec(txtDiscountPercentage.Text.Trim()),
                    DiscountAmount = CommonHelper.ToDec(txtDiscountAmount.Text.Trim()),
                    StartDate = DateApplyDatePicker.SelectedDate.Value,
                    CustomerId = this.CustomerID
                };
                this.DiscountService.DiscountSave(discount);
                this.DiscountBind();
            }
            catch (Exception exc)
            {
                ProcessException(exc);
            }
        }

        protected void gvDiscount_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvDiscount.PageIndex = e.NewPageIndex;
            this.DiscountBind();
        }

        protected void gvDiscount_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblPercentage = (Label)e.Row.FindControl("lblDiscountPercentage");
                lblPercentage.Text = String.Format("{0:P2}", CommonHelper.ToDec(lblPercentage.Text)/100);
            }
        }
    }
}